package com.xnyzc.lhy.common.util;

import com.xnyzc.lhy.common.exception.BusinessSilentException;
import lombok.SneakyThrows;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * @author luoqingwei
 **/
public class AssertUtil {

    public static void isTrue(boolean expression, String msg) throws BusinessSilentException {
        if (!expression) {
            throw new BusinessSilentException(msg);
        }
    }

    public static void isNullOrIsFalse(Object obj,boolean bool,String msg){
        if(Objects.nonNull(obj) && bool){
            throw new BusinessSilentException(msg);
        }
    }

    public static void isFalse(boolean expression, String msg) throws BusinessSilentException {
        if (expression) {
            throw new BusinessSilentException(msg);
        }
    }

    public static void isFalse(boolean expression, String msg,Object...format) throws BusinessSilentException {
        if (expression) {
            throw new BusinessSilentException(String.format(msg,format));
        }
    }

    public static void isNull(Object object, String msg) throws BusinessSilentException {
        if (object != null) {
            throw new BusinessSilentException(msg);
        }
    }

    public static void isNull(Object object, String msg,Object...format) throws BusinessSilentException {
        if (object != null) {
            throw new BusinessSilentException(String.format(msg,format));
        }
    }

    public static void notEmpty(Collection collection,String msg){
        if(CollectionUtils.isEmpty(collection)){
            throw new BusinessSilentException(msg);
        }
    }
    public static void isEmpty(Collection collection,String msg){
        if(CollectionUtils.isNotEmpty(collection)){
            throw new BusinessSilentException(msg);
        }
    }
    public static void isNotEmpty(Collection collection,String msg){
        if(CollectionUtils.isEmpty(collection)){
            throw new BusinessSilentException(msg);
        }
    }

    public static void isEmpty(Collection collection,String msg,Object...format){
        if(CollectionUtils.isNotEmpty(collection)){
            throw new BusinessSilentException(String.format(msg,format));
        }
    }

    public static void localDateTimeIsBeforeNow(LocalDateTime dateTime,String msg){
        if(Objects.nonNull(dateTime) && dateTime.isAfter(LocalDateTime.now())){
            throw new BusinessSilentException(msg);
        }
    }

    public static void notNull(Object object, String msg) throws BusinessSilentException {
        if (object == null) {
            throw new BusinessSilentException(msg);
        }
    }
    public static void gtZero(Long number, String msg) throws BusinessSilentException {
        if (number == null || number <= NumberUtils.LONG_ZERO) {
            throw new BusinessSilentException(msg);
        }
    }
    public static void isTrue(boolean expression, String msg,Object...format) throws BusinessSilentException {
        if (!expression) {
            throw new BusinessSilentException(String.format(msg,format));
        }
    }

    public static void notNull(Object object, String msg,Object...format) throws BusinessSilentException {
        if (object == null) {
            throw new BusinessSilentException(String.format(msg,format));
        }
    }

    public static void notBlank(String data,String msg){
        if(StringUtils.isBlank(data)){
            throw new BusinessSilentException(msg);
        }
    }

    public static void notBlank(String data,String msg,Object...format){
        if(StringUtils.isBlank(data)){
            throw new BusinessSilentException(String.format(msg,format));
        }
    }

    @SneakyThrows(value = Exception.class)
    public static void notNullThrow(Object object, Supplier<? extends Exception> supplier) {
        if (object == null) {
            throw supplier.get();
        }
    }

    @SneakyThrows(value = Exception.class)
    public static void notBlankThrow(String object, Supplier<? extends Exception> supplier) {
        if (StringUtils.isBlank(object)) {
            throw supplier.get();
        }
    }

    @SneakyThrows(value = Exception.class)
    public static void notBlank(String data, Supplier<? extends Exception> supplier){
        if(StringUtils.isBlank(data)){
            throw supplier.get();
        }
    }

}
